<template>
  <div>
    <h1>{{ id ? '编辑' : '新建' }}英雄</h1>
    <el-form label-width="100px" @submit.native.prevent="save">
      <el-tabs type="border-card">
        <el-tab-pane label="基础信息">
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthHeaders()" :show-file-list="false" :on-success="afterUpload">
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="英雄类型">
            <el-select v-model="model.categories" multiple>
              <el-option v-for="item in categories" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate :max="9" show-score style="margin-top:0.6rem" v-model="model.scores.difficult" multiple> </el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate :max="9" show-score style="margin-top:0.6rem" v-model="model.scores.skills" multiple> </el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate :max="9" show-score style="margin-top:0.6rem" v-model="model.scores.attack" multiple> </el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate :max="9" show-score style="margin-top:0.6rem" v-model="model.scores.survival" multiple> </el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple>
              <el-option v-for="item in items" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple>
              <el-option v-for="item in items" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usegeTips"> </el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips"> </el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input type="textarea" v-model="model.teamTips"> </el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能">
          <el-button type="text" @click="model.skills.push({})"> <i class="el-icon-plus"></i>添加技能</el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item, i) in model.skills" :key="i">
              <el-form-item label="技能名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload class="avatar-uploader" :action="url" :show-file-list="false" :on-success="(res) => $set(item, 'icon', res.url)">
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.desctiption" type="textarea"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="danger" @click="model.skills.splice(i, 1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top:1rem">
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {
        scores: {
          difficult: 0,
        },
        skills: [], //技能
      },
      categories: [], //英雄分类
      items: [], //装备
      url: 'http://192.168.135.114:3000/admin/api/upload',
    };
  },

  created() {
    this.id && this.fetch();
    this.fetchCategories();
    this.fetchItems();
    // this.fetchCategories();
  },

  methods: {
    async save() {
      if (!this.model.name) {
        this.$message({
          showClose: true,
          message: '请填写分类',
        });
        return;
      }
      let res;
      let message = '';
      // 根据id的有无 判断是新增还是修改
      if (this.id) {
        res = await this.axios.put(`rest/heroes/${this.id}`, this.model);
        message = '修改成功';
      } else {
        res = await this.axios.post('rest/heroes', this.model);
        message = '添加成功';
      }

      this.$message({
        showClose: true,
        message: message,
        type: 'success',
      });

      setTimeout(() => {
        this.$router.push('/heroes/list');
      }, 500);
    },

    async fetch() {
      const res = await this.axios.get(`rest/heroes/${this.id}`);
      // this.model = res.data;
      this.model = { ...this.model, ...res.data };
    },

    // 获取英雄分类
    async fetchCategories() {
      const res = await this.axios.get(`rest/categories`);
      this.categories = res.data;
    },

    // 出装
    async fetchItems() {
      const res = await this.axios.get(`rest/items`);
      this.items = res.data;
    },

    // 图片上传服务器成功
    afterUpload(res) {
      this.$set(this.model, 'avatar', res.url);
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 5rem;
  height: 5rem;
  line-height: 5rem;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style scoped></style>;
